<?php
/**
 * Created by PhpStorm.
 * User: Drunk
 * Date: 2016-12-11
 * Time: 13:10
 */

/**
 * simple the ? : to one function
 * @param mixed $input
 * @param mixed $emptyif
 * @return mixed
 */
function if_empty($input, $emptyif){
    return empty($input)
        ? $emptyif
        : $input;
}

/**
 * check if input is regexp
 * @param string $regexp
 * return bool
 */
function is_reg($regexp){
    return preg_match('/^\/.+?\/\w*$/', $regexp);
}

/**
 * http jump
 * @param string $url
 * @param bool|false $is_permanently
 */
function jump($url, $is_permanently = false){
    if( $is_permanently ) header('HTTP/1.1 301 Moved Permanently');
    header('Location: '.$url);
    die;
}

/**
 * json回应
 * @param bool|mixed $result
 * @param string $message
 * @param null|int $code
 * @return string
 */
function json_response($result, $message = '', $code = null){
    $json = ['result' => $result];
    if (!empty($message)) $json['message'] = $message;
    if (!is_null($code)) $json['code'] = $code;
    return die(json_encode($json));
}